package com.example.shop.api;

import com.example.shop.base.ApiBaseController;
import com.example.shop.base.Result;
import com.example.shop.commodity.entity.PayInfoDto;
import com.example.shop.commodity.entity.ShopCommodity;
import com.example.shop.commodity.entity.ShopOrder;
import com.example.shop.commodity.service.ShopCartService;
import com.example.shop.commodity.service.ShopCommodityService;
import com.example.shop.commodity.service.ShopOrderService;
import com.example.shop.common.utils.Constants;
import com.example.shop.common.utils.RedisService;
import com.example.shop.domain.wx.UserInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/api/shop/order")
public class ApiShopOrderController extends ApiBaseController {

    @Autowired
    private ShopCommodityService commodityService;
    @Autowired
    private ShopOrderService orderService;
    @Autowired
    private RedisService redisService;

    static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @GetMapping("/address")
    public Result getAddress() {
        UserInfo userInfo = (UserInfo) redisService.get(Constants.WX_API_TOKEN + getToken());
        String address = orderService.getAddress(userInfo.getUid());
        if (StringUtils.isBlank(address)) {
            return Result.error();
        }
        return Result.success(address);
    }

    @PostMapping("/del")
    public Result del(String id) {
        orderService.logicalDeletion(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result list(ShopOrder order) {
        UserInfo userInfo = (UserInfo) redisService.get(Constants.WX_API_TOKEN + getToken());
        order.setUid(userInfo.getUid());
        PageInfo<ShopOrder> pageInfo = orderService.findPage(order);
        Map<String, Object> map = null;
        List<Map<String, Object>> list = new ArrayList<>();
        for (ShopOrder shopOrder : pageInfo.getList()) {
            map = new HashMap<>();
            map.put("id", shopOrder.getId());
            String[] cids = shopOrder.getCid().split(",");
            List<Map<String, Object>> commodities = new ArrayList<>();
            Map<String, Object> map1 = null;
            for (String cid : cids) {
                String[] s = cid.split("-");
                map1 = new HashMap<>();
                ShopCommodity commodity = commodityService.get(s[0]);
                map1.put("id", s[0]);
                map1.put("name", commodity == null ? "-" : commodity.getName());
                map1.put("price", s[2]);
                map1.put("number", s[1]);
                map1.put("pic", picture_url + commodity.getPictures());
                commodities.add(map1);
            }
            map.put("status", shopOrder.getStatus());
            map.put("commodities", commodities);
            map.put("payPrice", shopOrder.getPayPrice());
            if (!order.getStatus().equals("0")) {
                map.put("payTime", sdf.format(shopOrder.getPayTime()));
                map.put("payType", shopOrder.getPayType());
            }
            list.add(map);
        }
        Map<String, Object> map1 = new HashMap<>();
        map1.put("isHasNextPage", pageInfo.isHasNextPage());
        map1.put("data", list);
        return Result.success(map1);
    }

    @PostMapping("/pay")
    public Result pay(PayInfoDto payInfoDto) throws JsonProcessingException {
        int i = orderService.pay(payInfoDto);
        if (i == 0) {
            return Result.error();
        }
        return Result.success(payInfoDto.getId());
    }

    @GetMapping("/get")
    public Result get(String id) {
        ShopOrder order = orderService.getById(id);
        if (null == order) {
            return Result.error("未查询到该订单");
        }
        String[] cids = order.getCid().split(",");
        List<Map<String, Object>> commodities = new ArrayList<>();
        Map<String, Object> map = null;
        for (String cid : cids) {
            String[] s = cid.split("-");
            map = new HashMap<>();
            ShopCommodity commodity = commodityService.get(s[0]);
            map.put("name", commodity == null ? "-" : commodity.getName());
            map.put("price", s[2]);
            map.put("number", s[1]);
            map.put("pic", picture_url + commodity.getPictures());
            commodities.add(map);
        }
        order.setCommodities(commodities);
        return Result.success(order);
    }

    @PostMapping("/add")
    public Result add(ShopOrder order) {
        UserInfo userInfo = (UserInfo) redisService.get(Constants.WX_API_TOKEN + getToken());
        return Result.success(orderService.createOrder(order, userInfo.getUid()));
    }

    @PostMapping("/add/{id}")
    public Result add(@PathVariable("id") String id) {
        UserInfo userInfo = (UserInfo) redisService.get(Constants.WX_API_TOKEN + getToken());
        return Result.success(orderService.createOrder(id, userInfo.getUid()));
    }

}
